from kgcar.spider.utils import get_page
from kgcar.spider.utils import PageRenderer
from lxml import etree
import re
import os
import csv


# 品牌列表
def parse_items(url):
    current_path = os.getcwd()
    file_name = current_path + "/../data/" + "brand.csv"
    with open(file_name, 'wt', newline='', encoding='utf-8') as csvfile:
        header = ['brand_id', 'brand_name', 'brand_count', 'brand_href']
        writer = csv.writer(csvfile)
        writer.writerow(header)

        # 访问页面
        renderer = PageRenderer()
        response = renderer.render_page(url)
        html = etree.HTML(response)
        cartree = html.xpath('//*[@id="cartree"]/ul/li')
        for selector in cartree:
            brand_id = selector.xpath('./@id')[0]
            brand_name = selector.xpath('normalize-space(./h3/a/text())')
            count_text = selector.xpath('./h3/a/em/text()')[0]
            count = re.findall("\d+", count_text)[0]
            href = selector.xpath('./h3/a/@href')[0]

            # 写入CSV文件
            csvrow = [brand_id, brand_name, count, href]
            writer.writerow(csvrow)




if __name__ == '__main__':
    print("Processing")
    url = 'https://car.autohome.com.cn/'
    parse_items(url)
    print("Processing End")


